package com.example.apple.tianqiyubaoone;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;

/**
 * Created by apple on 17/3/7.
 */

public class DrawingFunction extends View{
    private int mov_x;//声明起点坐标
    private int mov_y;
    private Paint paint;//声明画笔
    private Canvas canvas;//画布
    private Bitmap bitmap;//位图
    private int blcolor;
    int onepoint;
    ArrayList<String> maxTemperature ;
    ArrayList<String> minTemperature ;
    public DrawingFunction(Context context, ArrayList<String> maxTemperature, ArrayList<String> minTemperature) {
        super(context);
        this.maxTemperature =maxTemperature;
        this.minTemperature = minTemperature;
        paint=new Paint(Paint.DITHER_FLAG);//创建一个画笔
        bitmap = Bitmap.createBitmap(480, 854, Bitmap.Config.ARGB_8888);
        canvas=new Canvas();//设置位图的宽高
        canvas.setBitmap(bitmap);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);//锯齿不显示
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int min = biggestAndMinimumPoor();
        int bom = 78;
        int top =78;
        int middle = 77;
        paint.setStrokeWidth(13);
        float[]minh= new float[14];
        float[]maxh = new float[14];
      //  floats={}
        paint.setColor(Color.WHITE);
        for (int i =0;i<7;i++){
            int min1 = Integer.parseInt(minTemperature.get(i));
            minh[i*2+1]=236-(min1-min)*onepoint+78;
            minh[i*2]=77*(1+2*i);
        }
        canvas.drawPoints(minh,paint);
        for (int i =0;i<7;i++){
            int max1 = Integer.parseInt(maxTemperature.get(i));
            maxh[i*2+1]=236-(max1-min)*onepoint+78;
            maxh[i*2]=77*(1+2*i);
        }
        canvas.drawPoints(maxh,paint);
        paint.setStrokeWidth(5);
        paint.setColor(Color.YELLOW);
        canvas.drawLines(new float[]{minh[0],minh[1],minh[2],minh[3],minh[2],minh[3],minh[4],minh[5],minh[4],minh[5],minh[6],minh[7],minh[6],minh[7],minh[8],minh[9],minh[8],minh[9],minh[10],minh[11],minh[10],minh[11],minh[12],minh[13]},paint);
        paint.setColor(Color.BLUE);
        canvas.drawLines(new float[]{maxh[0],maxh[1],maxh[2],maxh[3],maxh[2],maxh[3],maxh[4],maxh[5],maxh[4],maxh[5],maxh[6],maxh[7],maxh[6],maxh[7],maxh[8],maxh[9],maxh[8],maxh[9],maxh[10],maxh[11],maxh[10],maxh[11],maxh[12],maxh[13]},paint);
      //  canvas.drawLines(new float[]{minh[7],minh[8],minh[9],minh[10]},paint);
        //canvas.drawBitmap(bitmap,0,0,null);
//        paint.setStrokeWidth(5);
//        paint.setTextSize(50);
//        canvas.drawText("TEXT",140,50,paint);
//        canvas.drawLine(0 ,0,154,100, paint);//画线  1077 394
//        paint.setStrokeWidth(15);
//        canvas.drawPoint(77,100 ,paint);//画点//77 131  78  236
          invalidate();

       // 距离上边框78 下边框78  每部分154 每部分中点77 每度高度26

    }
    public int biggestAndMinimumPoor(){
        int min;
        int max;
        int a;
       max= min =Integer.parseInt(minTemperature.get(0));
        for (int i=0;i<minTemperature.size();i++){
            if (min>Integer.parseInt(minTemperature.get(i))){
                min=Integer.parseInt(minTemperature.get(i));
            }
            if (max<Integer.parseInt(minTemperature.get(i))){
                max = Integer.parseInt(minTemperature.get(i));
            }
        }
        for (int i=0;i<maxTemperature.size();i++){
            if (min>Integer.parseInt(maxTemperature.get(i))){
                min=Integer.parseInt(maxTemperature.get(i));
            }
            if (max<Integer.parseInt(maxTemperature.get(i))){
                max = Integer.parseInt(maxTemperature.get(i));
            }
        }
       onepoint = 236/(max-min);
        Log.e("-->", String.valueOf(onepoint)+"->"+max+"->"+min);
        return min;
    }
}
